package cn.honglian.basic.controller;

import cn.honglian.basic.util.AjaxResult;
import cn.honglian.basic.util.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {

    /**
     * 上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart("file") MultipartFile file) throws IOException {
        /// 真实路径
        String originalFilename = file.getOriginalFilename();
        // 获取扩展名
        String extension = FilenameUtils.getExtension(originalFilename);
        // 执行上传
        String path = FastdfsUtil.upload(file.getBytes(), extension);
        return AjaxResult.me().success(path);
    }
    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path){
        String all = path.substring(1);
        String groupName = all.substring(0, all.indexOf("/"));
        String pathName = all.substring(all.indexOf("/")+1);

        FastdfsUtil.delete(groupName,pathName);
        return AjaxResult.me().success(path);

    }

}
